package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.data.Result;
import com.dongdongshop.data.ResultEnum;
import com.dongdongshop.pojo.seller.TbContentCategory;
import com.dongdongshop.service.ContentCategoryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.xml.ws.Response;
import java.util.List;

@Controller
@RequestMapping("contentCg")
public class ContentCategoryController {

    @Reference
    private ContentCategoryService contentCategoryService;

    private final Logger logger= LoggerFactory.getLogger(ContentCategoryController.class);

    @RequestMapping("toList")
    public String toList(){
        return "admin/content_category";
    }

    @RequestMapping("listCategory")
    @ResponseBody
    public Result listCategory(@RequestParam("name")String name){
        try {
            List<TbContentCategory> list=contentCategoryService.getName(name);
            return Result.response(ResultEnum.OK).setData(list);
        }catch (Exception e){
            logger.info("数据失败",e);
            return Result.response(ResultEnum.FAIL);
        }
    }

    @RequestMapping("addCategory")
    @ResponseBody
    public Result addCategory(TbContentCategory contentCategory){
        try {
            int i=contentCategoryService.addCategory(contentCategory);
            return Result.response(ResultEnum.OK).setData(i);
        }catch (Exception e){
            logger.info("数据失败",e);
            return Result.response(ResultEnum.FAIL);
        }
    }

    @RequestMapping("selectById")
    @ResponseBody
    public Result selectById(@RequestParam("id")Long id){
        try {
            TbContentCategory tbContentCategory=contentCategoryService.seleteById(id);
            return Result.response(ResultEnum.OK).setData(tbContentCategory);
        }catch (Exception e){
            logger.info("数据失败",e);
            return Result.response(ResultEnum.FAIL);
        }
    }

    @RequestMapping("updateCategory")
    @ResponseBody
    public Result updateCategory(TbContentCategory tbContentCategory){
        try {
            int i=contentCategoryService.update(tbContentCategory);
            return Result.response(ResultEnum.OK);
        }catch (Exception e){
            logger.info("数据失败",e);
            return Result.response(ResultEnum.FAIL);
        }
    }

    @RequestMapping("deleteCategory")
    @ResponseBody
    public Result deleteCategory(@RequestParam("ids[]")Long[] ids){
        try {
            int i=contentCategoryService.deleteById(ids);
            return Result.response(ResultEnum.OK).setData(i);
        }catch (Exception e){
            logger.info("数据失败",e);
            return Result.response(ResultEnum.FAIL);
        }
    }
}
